package ni;

import Presenter.Presenter;
import chatsystem.*;
import chatsystemTDa2.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ChatNI implements ToRemoteChatSystem, FromRemoteChatSystem {
    
    private Presenter presenter;
    
    private UDPSender _client;
    private UDPReceiver _server;
    private InetAddress _group;
    final int _port = 16050;
    boolean _actif;
    
    public ChatNI(Presenter p) throws SocketException, InterruptedException, UnknownHostException {
        presenter = p;
        _group = InetAddress.getByName("255.255.255.255");
        _actif = false;
        _server = new UDPReceiver(this);
        _server.start();
        _client = new UDPSender(this);  
    }
    
    public int getPort(){
        return _port;
    }
    
    public void blockNI(){
        _actif=false;
    }
    public void unblockNI(){
        _actif=true;
    }
    
    void messageHandle(Message m, String remoteAddr)
    {
        if(_actif){
            
            if(m instanceof Hello) {
                onHelloReceived((Hello) m, remoteAddr);
            }
            else if(m instanceof HelloAck) {
                onHelloAckReceived((HelloAck) m, remoteAddr);
            }
            else if(m instanceof Send) {
                onSendReceived((Send) m, remoteAddr);
            }
            else if(m instanceof SendAck) {
                onSendAckReceived((SendAck) m);
            }
            else if(m instanceof Goodbye) {
                onGoodbyeReceived((Goodbye) m, remoteAddr);
            }
        }
    }
    
    @Override
    public void sendHello(String nickname) {
        Message m;
        m = new Hello(nickname);
        _client.sendTo(m,_group.getHostAddress(),_port);
    }

    @Override
    public void sendHelloAck(String nickname, String hostname) {
        Message m;
        m = new HelloAck(nickname);
        _client.sendTo(m,hostname,_port);    
    }
    
    @Override
    public void sendSend(String text, int id, String hostname) {
        Message m;
        m = new Send(text, id);
        _client.sendTo(m,hostname,_port); 
    }

    @Override
    public void sendSendAck(int id_message, String hostname) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void sendGoodbye(String nickname) {
        Message m;
        m = new Goodbye(nickname);
        _client.sendTo(m,_group.getHostAddress(),_port); 
    }

    //@Override
    public void onHelloReceived(Hello m, String remoteAddr) {
            presenter.processHello(m,remoteAddr);
    }

    @Override
    public void onHelloAckReceived(HelloAck m, String remoteAddr) {
        presenter.processHelloAck(m,remoteAddr);
    }
    
    @Override
    public void onSendReceived(Send m, String remoteAddr) {
        presenter.processSend(m,remoteAddr);
    }
    
    @Override
    public void onSendAckReceived(SendAck m) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onGoodbyeReceived(Goodbye m, String remoteAddr) {
        presenter.processGoodbye(m,remoteAddr);
    }
}
